﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Services;
using Domain;

namespace ProductManager {
    public class CommandManager {

        //static IRepository repo = new Concrete.SqlRepository();
        IRepository repo;

        public CommandManager(IRepository repository) {
            this.repo = repository;
        }

        public void ExecuteCommand(string command) {
            switch (command) {
                case "LIST":
                    foreach (Product p in repo.GetAll()) {
                        WriteProductDetails(p);
                    }
                    break;
                case "GET":
                    Console.WriteLine("Please enter the ID for the product to view");
                    int id = int.Parse(Console.ReadLine());
                    Product single = repo.GetById(id);
                    WriteProductDetails(single);
                    break;
                case "ADD":
                    Product newproduct = CollectNewProductInformation();
                    repo.AddNew(newproduct);
                    Console.WriteLine("Product added");
                    break;
                default:
                    Console.WriteLine("Command not understood");
                    break;

            }
        }

        public static Product CollectNewProductInformation() {

            Product p = new Product();

            Console.WriteLine("Please enter the Name for the product and press enter:");
            p.Name = Console.ReadLine();

            Console.WriteLine("Please enter a Description for the product and press enter:");
            p.Description = Console.ReadLine();

            Console.WriteLine("Please enter a Price for the product and press enter:");
            p.Price = decimal.Parse(Console.ReadLine());

            return p;
        }

        private void WriteProductDetails(Product p) {
            if (p == null)
                Console.WriteLine("No such product exists");
            else
                Console.WriteLine(
                            "ID: {0}, Name: {1}, Description: {2}, Price: {3}",
                            p.ID,
                            p.Name,
                            p.Description,
                            p.Price.ToString("c")
                );
        }

        
    }
}
